package com.jsp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;






import com.jsp.bean.UserBean;
import com.jsp.util.DBUtil;

public class UserDao {

	public UserBean login(UserBean user) throws SQLException{
		
		Connection con=DBUtil.getConnection();
		
		PreparedStatement pstmt=con.prepareStatement("select name,password from userinformation where name=? and password=?");
		
		pstmt.setString(1, user.getName());
		
		pstmt.setString(2,user.getPassword());
		
		ResultSet rs=pstmt.executeQuery();
		
		UserBean newUser = null;
		
		if(rs.next()){
			newUser=new UserBean();
			newUser.setName(rs.getString("name"));
			newUser.setPassword(rs.getString("password"));
			System.out.println(newUser.getName());
			System.out.println(newUser.getPassword());
		}
		
		DBUtil.closeConn(rs, pstmt, con);
		return newUser;
		
	}
	public UserBean register(UserBean user) throws SQLException {
		Connection con=DBUtil.getConnection();
		String sql = "insert into userinformation(name,password,name1,number,address,idcard,job)values(?,?,?,?,?,?,?)";
		PreparedStatement state = con.prepareStatement(sql);
		state.setString(1,user.getName());
		state.setString(2,user.getPassword());
		state.setString(3,user.getName1());
		state.setString(4,user.getNumber());
		state.setString(5, user.getAddress());
		state.setString(6, user.getIdcard());
		state.setString(7, user.getJob());
		state.executeUpdate();
		state.close();
		con.close();
		System.out.println("kkk");
		return user;
	}
	
}
